package com.polaris.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.polaris.common.utils.RespBean;
import com.polaris.server.mapper.SysDeptMapper;
import com.polaris.server.mapper.SysUserJobMapper;
import com.polaris.server.mapper.SysUserMapper;
import com.polaris.server.pojo.SysDept;
import com.polaris.server.pojo.SysUserJob;
import com.polaris.server.pojo.SysUser;
import com.polaris.server.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author polaris
 * @since 2021-05-19
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    private final SysUserMapper userMapper;
    private final SysDeptMapper deptMapper;
    private final SysUserJobMapper userJobMapper;

    public SysUserServiceImpl(SysUserMapper userMapper, SysDeptMapper deptMapper, SysUserJobMapper userJobMapper) {
        this.userMapper = userMapper;
        this.deptMapper = deptMapper;
        this.userJobMapper = userJobMapper;
    }

    /**
     * 分页查询用户
     *
     * @param page
     * @return
     */
    @Override
    public IPage<SysUser> selectUserPage(Page<SysUser> page) {

        return userMapper.selectPageVo(page);
    }

    /**
     * 查询用户list
     *
     * @param page
     * @return
     */
    @Override
    public RespBean getUserList(Page<SysUser> page) {
        Map<String, Object> returnMap = new HashMap<>();
        IPage<SysUser> data = userMapper.selectPage(page, null);
        if(data.getTotal()>0){
            returnMap.put("total", data.getTotal());
            returnMap.put("data",data.getRecords());
            return RespBean.success("查询成功", returnMap);
        }
        else{
            return RespBean.error("查询结果为空");
        }
    }

    /**
     * 根据条件进行筛选获取list
     *
     * @param page
     * @param param
     * @return
     */
    @Override
    public RespBean getUserList(Page<SysUser> page, SysUser param) {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> beanToMap = BeanUtil.beanToMap(param,true,false);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(beanToMap,false);
        IPage<SysUser> data = userMapper.selectPage(page, queryWrapper);
        if(data.getTotal()>0){
            returnMap.put("total", data.getTotal());
            returnMap.put("data",data.getRecords());
            return RespBean.success("查询成功", returnMap);
        }
        else{
            return RespBean.error("查询结果为空");
        }
    }

    /**
     * 修改用户的所属部门
     *
     * @param uid
     * @param dept
     * @return
     */
    @Override
    public SysUser updateDept(Long uid, String dept) {
        Long deptId = deptMapper.selectOne(new QueryWrapper<SysDept>().eq("name", dept)).getDeptId();
        SysUser user = userMapper.selectById(uid);
        try{
            user.setDeptId(deptId);
            userMapper.updateById(user);
            return user;
        }
        catch (Exception e){
            return null;
        }
    }

    /**
     * 修改用户的职位
     *
     * @param uid
     * @param jid
     * @return
     */
    @Override
    public SysUser updateJob(Long uid, Long jid) {
        SysUserJob userJob = userJobMapper.selectOne(new QueryWrapper<SysUserJob>().eq("user_id", uid));
        try{
            userJob.setJobId(jid);
            userJobMapper.updateById(userJob);
            return userMapper.selectById(uid);
        }
        catch (Exception e){
            log.error(String.valueOf(e));
        }
        return null;
    }

    /**
     * 修改手机号
     *
     * @param oldUser
     * @param phone
     * @return
     */
    @Override
    public SysUser updatePhone(SysUser oldUser, String phone) {
        try{
            oldUser.setPhone(phone);
            userMapper.updateById(oldUser);
            return oldUser;
        }
        catch (Exception e){
            log.error(e.getMessage());
            return null;
        }
    }
}
